/*
	PlAr is Platform Arena: a 2D multiplayer shooting game
	Copyright (c) 2010, Antonio Ragagnin <spocchio@gmail.com>
    All rights reserved.

    This file is licensed under the New BSD License.
*/
package demo;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.JFrame;
import plar.ClientServer.Server;
import plar.core.Common;
import plar.core.Level;
import plar.core.IniProperties;
import java.awt.Point;

public class server extends javax.swing.JFrame {




    static boolean silent=false;
	
	
	
    public static void main(String[] args) throws IOException {
	
        Common.intro();

        ArrayList <String> guns=Common.getJavas(Common.gunsFolder);
        ArrayList <String> players=Common.getJavas(Common.playersFolder);
		
		IniProperties inis = Common.getConfig();
        Server s = new Server();
		s.resolution.x=Integer.parseInt(inis.getProperty("resolutionx") );
		s.resolution.y=Integer.parseInt(inis.getProperty("resolutiony") );
        String response = new String();
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("--noGui"))
                silent = true;
            else if(response.equals(""))
                response = args[i];
        }
		String topic="";
		String lasd=new String(topic);
        if(!silent)
        {


            ArrayList <String> ll = Common.getJavas(Common.levelFolder);
            String lista = Common.joinAL(Common.getDescriptions(ll,Common.levelPackage));

		
			
            response = inis.getProperty("level");
			topic="Welcome to "
                                  + Common.serverName + "\n\n"
                                  + "The following players were found:\n"+Common.joinAL(players)+"\n"
                                  + "The following guns were found:\n"+Common.joinAL(guns)+"\n"
                                  + "Here the aviable levels:\n"+lista+"\n"
                                  + "\n"
                                  + "According to "+Common.configFile+"\n"
                                  + "The Level to load is "+inis.getProperty("level")+"\n"
                                  + "Screen resolution "+inis.getProperty("resolutionx")+"x"+inis.getProperty("resolutiony")+" px\n"
                                  + "Game resolution "+inis.getProperty("screenx")+"x"+inis.getProperty("screeny")+" m\n"
                                  + "Match lasts "+inis.getProperty("time")+" min\n"
                                  + "\n"
                                  + "\n"
                                  + "Press OK to start\n";
								  
			Common.message(topic,false,false);

            if (response.equals(""))
                response = ll.get(0);
            if(response.contains(".")) response = "LevelLoader " + response;
            response = Common.levelPackage + response;
        }
        Level l = loadLevel(response);


        if (l == null)

            Common.message("Failed loading the class "+ response, true,silent);



        s.newGame(l);
        s.loadGame();

        s.g.gunList = guns;
        s.g.playerList = players;
		s.g.setResolution(new Float(inis.getProperty("screenx") ),new Float(inis.getProperty("screeny") ));



        // s.g.resolution = new Point(800,600);
        for (int i = 0; i < args.length; i++) {
            if (args[i].equals("--noGui"))
                silent = true;
            // Common.info("["+args[i]+"]");
        }
        if (s.listen(Common.port)) {
            if (!silent) {
                java.awt.EventQueue.invokeLater(new Runnable() {
				
				
                    public void run() {
                        new server().setVisible(true);
						
                    }
                });
            }
        } else {
            s.close();
            System.exit(0);
        }

    }

    public server() {
	
        initComponents();
    }
  



    private void initComponents() {
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		
		
        JLabel l =  new JLabel("Server is running ..."); 
		
        getContentPane().add(l);

        pack();
        setSize(l.getWidth(), l.getHeight() + 50);

    }

    private static Level loadLevel(String s) {
        String[] split = s.split(" ");
        Level l;
        try {
            @SuppressWarnings("unchecked")
            Class c = Class.forName(split[0]);
            l = (Level) c.newInstance();
            
        } catch (Exception e) {
            return null;
        } catch (java.lang.NoClassDefFoundError e) {
            return null;
        }
        return l;

    }

}
